package com.duan.laugh.event.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duan.laugh.common.core.util.Assert;
import com.duan.laugh.common.core.util.Query;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.event.pojo.entity.EventHandlerRule;
import com.duan.laugh.event.pojo.vo.EventHandlerRuleVO;
import com.duan.laugh.event.pojo.vo.EventModelVO;
import com.duan.laugh.event.service.IEventHandlerRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 事件处理规则 controller
 * </p>
 *
 * @author liutf
 * @since 2019-11-12
 */
@Api(tags = "事件处理规则")
@RestController
@RequestMapping("/event-handler-rule")
public class EventHandlerRuleController {
    @Autowired
    private IEventHandlerRuleService eventHandlerRuleService;

    /**
     * 新增事件处理规则
     *
     * @param eventHandlerRule 实体
     * @return true/false
     */
    @PostMapping
    @ApiOperation("新增事件处理规则")
    public R save(@RequestBody EventHandlerRule eventHandlerRule) {
        return R.ok(eventHandlerRuleService.save(eventHandlerRule));
    }

    /**
     * 根据id查看事件处理规则
     *
     * @param id
     * @return EventHandlerRule
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看事件处理规则")
    public R<EventHandlerRule> getById(@PathVariable String id) {
        return R.ok(eventHandlerRuleService.getById(id));
    }

    /**
     * 分页查询事件处理规则
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @ApiOperation("分页查询事件处理规则")
    public IPage<EventHandlerRule> page(@RequestParam Map<String, Object> params) {
        return eventHandlerRuleService.page(new Query<>(params));
    }

    /**
     * 根据id删除事件处理规则
     *
     * @param id
     * @return true/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除事件处理规则")
    public R removeById(@PathVariable String id) {
        return R.ok(eventHandlerRuleService.removeById(id));
    }

    /**
     * 根据id修改事件处理规则
     *
     * @param eventHandlerRule 实体
     * @return true/false
     */
    @PutMapping
    @ApiOperation("根据id修改事件处理规则")
    public R updateById(@RequestBody EventHandlerRule eventHandlerRule) {
        return R.ok(eventHandlerRuleService.updateById(eventHandlerRule));
    }

    /**
     * 0010206 批量新增事件处理规则
     *
     * @param eventModelVO 实体
     * @return true/false
     */
    @PostMapping("/batch")
    @ApiOperation(value = "0010206 批量新增事件处理规则", notes = "参数：{'id':'','eventHandlerRules':[]}")
    public R<Boolean> insertByEventModelVO(@RequestBody EventModelVO eventModelVO) {
        return R.ok(eventHandlerRuleService.insertByEventModelVO(eventModelVO));
    }

    /**
     * 0010207 批量根据id修改事件处理规则
     *
     * @param eventHandlerRuleVOS 实体list
     * @return true/false
     */
    @PutMapping("/batch")
    @ApiOperation("0010207 批量根据id修改事件处理规则")
    public R<Boolean> updateByEventModelVO(@RequestBody List<EventHandlerRuleVO> eventHandlerRuleVOS) {
        return R.ok(eventHandlerRuleService.updateByEventModelVO(eventHandlerRuleVOS));
    }

    /**
     * 0010208 根据事件模型ids批量删除事件处理规则
     *
     * @param params
     * @return true/false
     */
    @DeleteMapping("/batch/model")
    @ApiOperation("0010208 根据事件模型ids批量删除事件处理规则")
    public R<Boolean> deleteByEventModelIds(@RequestBody Map<String, Object> params) {
        String ids = getParamString(params, "ids");
        return R.ok(eventHandlerRuleService.deleteByEventModelIds(ids));
    }

    /**
     * 0010209 根据ids批量删除事件处理规则
     *
     * @param params
     * @return true/false
     */
    @DeleteMapping("/batch")
    @ApiOperation("0010209 根据ids批量删除事件处理规则")
    public R<Boolean> removeById(@RequestBody Map<String, Object> params) {
        String ids = getParamString(params, "ids");
        return R.ok(eventHandlerRuleService.deleteByIds(ids));
    }

    /**
     * 获取map里的value
     *
     * @param params map
     * @param param  目标
     * @return string
     */
    private String getParamString(Map params, String param) {
        String value = String.valueOf(params.get(param));
        Assert.isBlank(value, param + "不能为空");
        return value;
    }
}

